import * as Router from 'koa-router';
import {koaBody} from 'koa-body'
import { SunchartModule } from '../module/sunchart';
import { DataExporter } from '../module/exportData';
import { UserModule } from '../module/user';
//用来签名的密钥

const router = new Router();

router.prefix('/api')

router.post("/loadByName",koaBody(), async ctx => {
  const { body } = ctx.request as any;
  //登录逻辑，略，即查找数据库，若该用户和密码合法，即将其信息生成一个JWT令牌传给用户
  let result = await SunchartModule.ins.loadByName(body);
  ctx.body = {
    ret:0,
    data:result
  };
});
 
router.post("/save",koaBody(), async ctx => {
    const { body } = ctx.request as any;
    //登录逻辑，略，即查找数据库，若该用户和密码合法，即将其信息生成一个JWT令牌传给用户
    let result = await SunchartModule.ins.save(body);
    ctx.body = {
      ret:0,
      data:result
    };
});


router.post("/exportToExcel",koaBody(), async ctx => {
  const { body } = ctx.request as any;
  //导出全表，按需求分表
  let result = await DataExporter.ins.exportToExcel(body);
  ctx.body = {
    ret:0,
    data:result
  };
});

router.post("/exportRawToExcel",koaBody(), async ctx => {
  const { body } = ctx.request as any;
  //导出全表
  let result = await DataExporter.ins.exportRawToExcel(body);
  ctx.body = {
    ret:0,
    data:result
  };
});


router.post("/login",koaBody(), async ctx => {
  const { body } = ctx.request as any;
  //导出全表
  let result = await UserModule.ins.loginByName(body);
  ctx.body = {
    ret:0,
    data:result
  };
});


export default router
